package com.ruoyi.common.apiResult;

import io.netty.channel.Channel;
import io.netty.channel.ChannelId;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.concurrent.GlobalEventExecutor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

@Component
public class ChannelSupervise {
	private   static ChannelGroup GlobalGroup=new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
	private  static ConcurrentMap<String, ChannelId> ChannelMap=new ConcurrentHashMap();
	private  static Map<String, Set<Channel>> map = new HashMap<>();
	private  static Map<String, String> mapUri = new HashMap<>();
	FullHttpRequest req ;
	public  static void addChannel(Channel channel){
		GlobalGroup.add(channel);
		ChannelMap.put(channel.id().asShortText(),channel.id());
	}
	public static void removeChannel(Channel channel){
		GlobalGroup.remove(channel);
		ChannelMap.remove(channel.id().asShortText());
		remoUri(channel);
	}
	public static  Channel findChannel(String id){
		return GlobalGroup.find(ChannelMap.get(id));
	}
	public static void sendChannelAll(String msg,Channel channel){
		String uri = mapUri.get(channel.id().asShortText());
		Set<Channel> list = map.get(uri);
		if(list!=null){
			for (Channel chSend:list
			) {
				chSend.writeAndFlush(new TextWebSocketFrame(msg));

			}
		}
//        GlobalGroup.writeAndFlush(tws);
	}
	public static void sysSendAll(String msg){
		GlobalGroup.writeAndFlush(new TextWebSocketFrame(msg));
	}
	//按路径发消息
	public static void uriSend(String uri,String msg){
		Set<Channel> list = map.get(uri);
		if(list!=null){
			for (Channel chSend:list
			) {
				chSend.writeAndFlush(new TextWebSocketFrame(msg));

			}
		}
	}
	public  static int addUri(Channel channel,
							  String uri){
		Set<Channel> list = map.get(uri);
		if(list==null)
			list = new HashSet<>();
		list.add(channel);
		map.put(uri,list);
		mapUri.put(channel.id().asShortText(),uri);
		Set<Channel> finalList = list;
//        Thread t1 = new Thread(new Runnable() {
//            @SneakyThrows
//            @Override
//            public void run() {
//                Thread.sleep(100);
//                Map msg = new HashMap();
//                Map ms= new HashMap();
//                ms.put("watchNum", Math.round(finalList.size()*2.3f+10));
//                msg.put("type",3);
//                msg.put("data",ms);
//                uriSend(uri, JSON.toJSONString(msg));
//            }
//        });
//        t1.start();
		return finalList.size();
	}
	public  static void remoUri(Channel channel){
		String uri = mapUri.get(channel.id().asShortText());
		Set<Channel> list = map.get(uri);
		if(list!=null)
			list.remove(channel);
		mapUri.remove(channel.id().asShortText());
	}
}
